Skip to content

CPP: add a query for catching alloca in a loop#965

Merged
geoffw0 merged 1 commit into
github:masterfrom
evverx:alloca-in-a-loop
Feb 22, 2019
Merged

CPP: add a query for catching alloca in a loop#965
geoffw0 merged 1 commit into
github:masterfrom
evverx:alloca-in-a-loop

Conversation

@evverx
Copy link
Copy Markdown
Contributor

@evverx evverx commented Feb 21, 2019

Thanks to Sam Lanning (@samlanning) and Robert Marsh (@rdmarsh2) for taking the time to help
to make it possible. In fact, it was Robert Marsh who effectively
wrote the query and figured out that __builtin_alloca should be
used to also take functions like strdupa into account. I just
filled out the metadata :-)

@samlanning is my understanding correct that we kind of agreed that the query is perfect and self-documenting so there's no need for me to add tests or documentation? :-)

@evverx evverx requested a review from a team as a code owner February 21, 2019 16:31
@ghost
Copy link
Copy Markdown

ghost commented Feb 21, 2019

CLA assistant check
All committers have signed the CLA.

@s0 s0 added the C++ label Feb 21, 2019
@evverx
Copy link
Copy Markdown
Contributor Author

evverx commented Feb 21, 2019

Forgot to add a link to the query in action: https://lgtm.com/query/6338234238766417258/

Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @evverx!

Results of this query on 42 projects on LGTM: https://lgtm.com/query/5428973981691801120/ . They appear to be correct from the point of view of what the query is trying to flag, but in some cases because the loops are bounded I think the code is actually safe.

With @precision high these results would be displayed on LGTM by default, which is a high standard for a new query. I'd be happier if we set it to @precision medium or spend longer refining the query.

where getAnEnclosingLoopOfExpr(fc) = l
and fc.getTarget().getName() = "__builtin_alloca"
and not l.(DoStmt).getCondition().getValue() = "0"
select l, fc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a proper message in the select. For example:

select fc, "Stack allocation is inside a $@ and could lead to overflow.", l, l.toString()

Thanks to Sam Lanning (@samlanning) and Robert Marsh for taking the time to help
to make it possible. In fact, it was Robert Marsh who effectively
wrote the query and figured out that __builtin_alloca should be
used to also take functions like strdupa into account. I just
filled out the metadata :-)
@s0
Copy link
Copy Markdown
Contributor

s0 commented Feb 21, 2019

@samlanning is my understanding correct that we kind of agreed that the query is perfect and self-documenting so there's no need for me to add tests or documentation? :-)

Haha, don't know about perfect ;) I think what I meant was that @rdmarsh2 offered to do the qhelp and unit tests. :)

@evverx
Copy link
Copy Markdown
Contributor Author

evverx commented Feb 21, 2019

@geoffw0 thank you for the review! Fair enough, "high" was probably more about what I aspire to :-) I changed the precision to "medium" and updated the select as you suggested.

@rdmarsh2
Copy link
Copy Markdown
Contributor

I'll let @geoffw0 finish up the review here and do tests and qhelp as a followup PR; @evverx do you mind if I ask for your review on that PR?

@evverx
Copy link
Copy Markdown
Contributor Author

evverx commented Feb 21, 2019

@rdmarsh2 not at all. I'm not sure at this point I'll be of any help but I think it would be useful for me to take a look at how all the moving parts should be put together to complete the query. And thank you again for your help!

Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets merge this then. 👍

@geoffw0 geoffw0 merged commit 8302ac4 into github:master Feb 22, 2019
@evverx evverx deleted the alloca-in-a-loop branch February 22, 2019 12:53
@evverx
Copy link
Copy Markdown
Contributor Author

evverx commented Feb 22, 2019

Thank you!

How long does it usually take for a query to be rolled out on LGTM? If I understand correctly the results will be hidden by default once the query has reached LGTM. Would it be safe to include it in .lgtm.yaml as per https://lgtm.com/help/lgtm/showing-hiding-query-results right now?

@sj
Copy link
Copy Markdown
Contributor

sj commented Feb 22, 2019

There's a continuous testing and deployment (and data migration) cycle of the QL analysis engine and default queries on LGTM.com. Once a query has been merged, it'll become part of that cycle.

Yesterday we started running a smoke test for the next deployment: the next version of the analysis engine is being tested on a subset of the ~130,000 LGTM.com projects. Results will be double-checked, and a full test (on all 130k projects) will likely be run early next week, after which the new engine will be made live.

Your query is unfortunately not part of the current cycle that started yesterday, which means it'll be part of the next cycle which should result in your query becoming live on LGTM.com in about two weeks.

Of course it's possible to run the query as a custom query on LGTM.com in the meantime; more info here: https://lgtm.com/help/lgtm/writing-custom-queries

@aibaars
Copy link
Copy Markdown
Contributor

aibaars commented Feb 22, 2019

@evverx It is safe to include the query in the .lgtm.yml file even though it does not exist yet on LGTM.com. Query include or exclude rules with query identifiers that do not (yet) exist have no effect and are simply ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants